package com.bw.auth.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
/**
 * @Author：jackma
 * @name：JackMaIpUtils
 * @Date：2023/12/29 23:00
 */
 
public class JackMaIpUtils {
    public static String getIp(){
        String url = "http://myip.ipip.net";
        String ipAddress = sendGetRequest(url);
        String Ip = ipAddress.substring(ipAddress.indexOf("当前 IP：")+6, ipAddress.indexOf("  来自于："));
        String addr = ipAddress.substring(ipAddress.indexOf("  来自于：") + 6);
        return Ip;
    }
    public static String getIpAddress(){
        String url = "http://myip.ipip.net";
        String ipAddress = sendGetRequest(url);
        String Ip = ipAddress.substring(ipAddress.indexOf("当前 IP：")+6, ipAddress.indexOf("  来自于："));
        String addr = ipAddress.substring(ipAddress.indexOf("  来自于：") + 6);
        return addr;
    }
    private static String sendGetRequest(String url) {
        StringBuilder response = new StringBuilder();
 
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
            con.setRequestMethod("GET");
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            int responseCode = con.getResponseCode();
            System.out.println("Response Code: " + responseCode);
 
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
 
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return response.toString();
    }
}